//
//  IntroViewController.m
//  CargocnRun
//
//  Created by mac on 2017/12/6.
//  Copyright © 2017年 mac. All rights reserved.
//

#import "IntroViewController.h"

@interface IntroViewController ()<UIScrollViewDelegate>
@property (nonatomic,strong) UIScrollView *scrollView;
@property (nonatomic,strong) NSArray *picsArray;
@property (nonatomic,strong) UIPageControl *pageControl;
@property (nonatomic,strong) UIButton *skipBtn;
@property (nonatomic,strong) UIButton *joinBtn;
@end

@implementation IntroViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self createScrollView];
}
-(void)createScrollView
{
    self.scrollView = [[UIScrollView alloc] initWithFrame:self.view.bounds];
    self.scrollView.contentSize = CGSizeMake(ScreenWidth*4, self.view.bounds.size.height);
    self.scrollView.userInteractionEnabled = YES;
//    self.scrollView.contentOffset = CGPointMake(ScreenWidth, 0);
    self.scrollView.pagingEnabled = YES;
    self.scrollView.delegate = self;
    self.scrollView.showsVerticalScrollIndicator = FALSE;
    self.scrollView.showsHorizontalScrollIndicator = FALSE;
    [self.view addSubview:self.scrollView];
    
    self.picsArray = @[@"guid01",@"guid02",@"guid03",@"guid04"];
    for (int i = 0; i<self.picsArray.count; i++)
    {
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0+i*ScreenWidth, 0, ScreenWidth, self.view.bounds.size.height)];
        imageView.image = [[UIImage imageNamed:[self.picsArray objectAtIndex:i]] imageWithRenderingMode:UIImageRenderingModeAutomatic];
        imageView.userInteractionEnabled = YES;
        [self.scrollView addSubview:imageView];
        
        if (i != 3)
        {
            self.skipBtn = [UIButton buttonWithType:UIButtonTypeCustom];
            self.skipBtn.frame =CGRectMake(self.view.bounds.size.width-50*ScaleRate, 20*ScaleRate, 40*ScaleRate, 40*ScaleRate);
            [self.skipBtn setTitle:@"跳过" forState:UIControlStateNormal];
            self.skipBtn.layer.cornerRadius = 20*ScaleRate;
            self.skipBtn.layer.masksToBounds = YES;
            self.skipBtn.titleLabel.font = [UIFont systemFontOfSize:12];
            [self.skipBtn setBackgroundColor:RGBACOLOR(110, 110, 110, 0.5)];
            [self.skipBtn addTarget:self action:@selector(toRootViewControll) forControlEvents:UIControlEventTouchUpInside];
//            [imageView addSubview:self.skipBtn];
        }
        else
        {
            self.joinBtn = [UIButton buttonWithType:UIButtonTypeCustom];
            self.joinBtn.frame =CGRectMake(70*ScaleRate,imageView.bounds.size.height-90*ScaleRate, imageView.bounds.size.width-140*ScaleRate, 40*ScaleRate);
            [self.joinBtn setTitle:@"立即体验" forState:UIControlStateNormal];
            [self.joinBtn setTitleColor:KBLUECOLOR forState:UIControlStateNormal];
            self.joinBtn.titleLabel.font = [UIFont systemFontOfSize:20];
            self.joinBtn.layer.cornerRadius = 15*ScaleRate;
            self.joinBtn.layer.masksToBounds = YES;
            self.joinBtn.layer.borderColor = RGBCOLOR(254, 108, 111).CGColor;
            self.joinBtn.layer.borderWidth = 2;
            self.joinBtn.titleLabel.font = [UIFont systemFontOfSize:14];
            [self.joinBtn setBackgroundColor:[UIColor clearColor]];
            [self.joinBtn addTarget:self action:@selector(toRootViewControll) forControlEvents:UIControlEventTouchUpInside];
            [imageView addSubview:self.joinBtn];
        }
    }
    //设置
    
    self.pageControl.numberOfPages=4; //设置页数为2
    
    self.pageControl.currentPage=0; //初始页码为 0
    
    self.pageControl.userInteractionEnabled=NO; //pagecontroller不响应点击操作
    
//    self.pageControl.alpha=0; //设置pageController 不可见
}
-(void)toRootViewControll
{
    [[AppDelegate sharedInstance] gotoRootController];
}
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    
    
    if(scrollView==self.scrollView){
        
        
        
        CGPoint offset = scrollView.contentOffset;
        ILog(@"%f---%f---%d",offset.x,self.view.bounds.size.width,(int)offset.x/(int)self.view.bounds.size.width);
        self.pageControl.currentPage = offset.x / (self.view.bounds.size.width); //计算当前的页码
        ILog(@"%ld",self.pageControl.currentPage);
        
    }
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
